/*
* Copyright 2012 Steven Swor.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cameljamod.demo.device;
import javax.swing.JFormattedTextField;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import net.wimpi.modbus.procimg.InputRegister;
import net.wimpi.modbus.procimg.SimpleInputRegister;
/**
*
* @author Steven Swor
*/
public class InputRegisterPanel extends javax.swing.JPanel implements InputRegister {
private int referenceAddress;
private final SimpleInputRegister valueHolder;
/**
* Creates new form DigitalInputPanel
*/
public InputRegisterPanel(final int referenceAddress, final int initialValue) {
valueHolder = new SimpleInputRegister(initialValue);
initComponents();
((SpinnerNumberModel) valueSpinner.getModel()).addChangeListener(new ChangeListener() {
public void stateChanged(final ChangeEvent e) {
valueHolder.setValue(((Integer) valueSpinner.getValue()).intValue());
}
});
JFormattedTextField tf = ((JSpinner.DefaultEditor) valueSpinner.getEditor()).getTextField();
tf.setFormatterFactory(HexWordFormatterFactory.getInstance());
setReferenceAddress(referenceAddress);
}
public final int getReferenceAddress() {
return referenceAddress;
}
public final void setReferenceAddress(final int referenceAddress) {
this.referenceAddress = referenceAddress;
referenceAddressLabel.setText(String.valueOf(referenceAddress));
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
referenceAddressLabel = new javax.swing.JLabel();
valueSpinner = new javax.swing.JSpinner();
referenceAddressLabel.setText("referenceAddress");
add(referenceAddressLabel);
valueSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), null, Integer.valueOf(65535), Integer.valueOf(1)));
add(valueSpinner);
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel referenceAddressLabel;
private javax.swing.JSpinner valueSpinner;
// End of variables declaration//GEN-END:variables
public final void setValue(final int value) {
valueSpinner.setValue(Integer.valueOf(value));
}
public final int getValue() {
return valueHolder.getValue();
}
public byte[] toBytes() {
return valueHolder.toBytes();
}
public short toShort() {
return valueHolder.toShort();
}
public int toUnsignedShort() {
return valueHolder.toUnsignedShort();
}
}